package com.hxf.web.action.meal;

import java.io.IOException;
import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.hxf.common.DateUtil;
import com.hxf.common.GlobalMPURL;
import com.hxf.common.JsonUtils;
import com.hxf.common.PublicUtil;
import com.hxf.dto.AreaDto;
import com.hxf.dto.MealBoxDto;
import com.hxf.dto.MealStyleDto;
import com.hxf.dto.RestaurantDto;
import com.hxf.dto.ReturnData;
import com.hxf.exception.ServiceException;
import com.hxf.web.action.WebActionBase;

/**
 * 餐厅简介
 * @author tengyicheng
 *
 */
@Results({@Result(name = "success", location = "../common/success.jsp"), @Result(name = "error", location = "../common/error.jsp"), @Result(name = "msg", location = "../common/msg.jsp")})
public class MealAction extends WebActionBase {
	
	private static final long serialVersionUID = 1L;

	private RestaurantDto restaurantDto;
	
	private List<AreaDto> areaList;
	private List<MealStyleDto> mealStyleList;
	
	private String parentCode;
	private Integer id;
	private String name;
	private String images;
	private Integer type;
	private Integer peopleNumber;
	private Double lowConsume; 
	private String remark;
	private Integer status;
	private String explanation;
	private String linkman;
	
	/**
	 * 餐厅简介
	 * @return
	 */
	public String mealInfo() {
		//菜单权限验证
		//if (!menuPrivilegeValidate(GlobalMPURL.MEAL_PRIVILEGE_URL)) {
		//	return "msg";
		//}
		 try {
			 Object adminUserDto = getSession().getAttribute("adminUserDto"); //联盟管理员Session
			 Object restaurentUserDto = getSession().getAttribute("restaurentUserDtoLogin");//餐厅 Session
				
			//查询餐厅信息
			 RestaurantDto restDto = new RestaurantDto();
			 if(null != adminUserDto){
				 restDto.setUserId(getAdminUser().getId());
			 }else if(null != restaurentUserDto){
				 if(PublicUtil.isNotNull(getSession().getAttribute("restaurantId"))){
					 restDto.setId(Integer.valueOf(getSession().getAttribute("restaurantId").toString()));
				 }
			 }
			 
			List<RestaurantDto> listRestaurantDto = (List<RestaurantDto>)serviceFacade.getRestaurantService().findByRestaurantName(restDto);
			 //查询地区
			areaList = super.serviceFacade.getAreaService().findByParentCode("0");
			//查询菜系
			mealStyleList = super.serviceFacade.getMealStyleService().findByMealStyle();
			if (listRestaurantDto != null) {
				restaurantDto = listRestaurantDto.get(0);
				getSession().setAttribute("imagePath", restaurantDto.getImages1());
				getSession().setAttribute("imagePath2", restaurantDto.getImages2());
				getSession().setAttribute("imagePath3", restaurantDto.getImages3());
			}
		 } catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		}
		return "restaurant-info";
	}
	
	/**
	 * 查询省，市，区,街
	 * @return
	 */
	public String queryArea() {
		try {
			areaList = super.serviceFacade.getAreaService().findByParentCode(parentCode);
			println(JsonUtils.listToJson(areaList));
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		} catch (IOException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	/**
	 * 保存餐馆简介信息
	 * @return
	 */
	public String restaurantSave() {
		//菜单权限验证
		//if (!menuPrivilegeValidate(GlobalMPURL.MEAL_PRIVILEGE_URL)) {
		//	return "msg";
		//}
		//查询餐厅信息RestaurantDto
		try {
			RestaurantDto rdto = null;
			if(null == rdto){
				Integer id = (Integer) getSession().getAttribute("restaurantId");
				rdto = (RestaurantDto)super.serviceFacade.getRestaurantService().get(RestaurantDto.class, id);
			}
			
			Object adminUserDto = getSession().getAttribute("adminUserDto"); //联盟管理员Session
			Object restaurentUserDto = getSession().getAttribute("restaurentUserDtoLogin");//餐厅 Session 
			List<RestaurantDto> listRdto = null;
			
			if(null != adminUserDto){
				listRdto = (List<RestaurantDto>)serviceFacade.getRestaurantService().findByRestaurantId(getAdminUser().getId());
			}else if(null != restaurentUserDto){
				listRdto = (List<RestaurantDto>)serviceFacade.getRestaurantService().findByRestaurantId(getRestaurentUserDto().getId());
			}
			
			
			Object imagePath = getSession().getAttribute("imagePath");
			Object imagePath2 = getSession().getAttribute("imagePath2");
			Object imagePath3 = getSession().getAttribute("imagePath3"); 
			if (null != imagePath) {
				restaurantDto.setImages1(imagePath.toString());
			} 
			if (null != imagePath2) {
				restaurantDto.setImages2(imagePath2.toString());
			} 
			if (null != imagePath3) {
				restaurantDto.setImages3(imagePath3.toString());
			}
			
			restaurantDto.setExplanation(explanation);//餐厅介绍
			
			if (listRdto == null) {
				rdto = listRdto.get(0);
				if(null != adminUserDto){
					restaurantDto.setUserId(getAdminUser().getId());
				}else if(null != restaurentUserDto){
					restaurantDto.setUserId(getRestaurentUserDto().getId());
				}
				super.serviceFacade.getRestaurantService().save(restaurantDto);// 插入
			} else {
				 restaurantDto.setId(rdto.getId());
				 restaurantDto.setUserId(rdto.getUserId());
				 super.serviceFacade.getRestaurantService().update(restaurantDto);// 修改
			}
			getSession().setAttribute("imagePath", null);
			getSession().setAttribute("imagePath2", null);
			getSession().setAttribute("imagePath3", null);
			getSession().removeAttribute("imagePath");
			getSession().removeAttribute("imagePath2");
			getSession().removeAttribute("imagePath3");
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		}
		requestURL = "meal!mealInfo.html";
		return SUCCESS;
	}
	
	public RestaurantDto getRestaurantDto() {
		return restaurantDto;
	}

	public void setRestaurantDto(RestaurantDto restaurantDto) {
		this.restaurantDto = restaurantDto;
	}
	
	public List<AreaDto> getAreaList() {
		return areaList;
	}
	
	public void setAreaList(List<AreaDto> areaList) {
		this.areaList = areaList;
	}
	
	public String getParentCode() {
		return parentCode;
	}
	
	public void setParentCode(String parentCode) {
		this.parentCode = parentCode;
	}
	
	public List<MealStyleDto> getMealStyleList() {
		return mealStyleList;
	}
	
	public void setMealStyleList(List<MealStyleDto> mealStyleList) {
		this.mealStyleList = mealStyleList;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getImages() {
		return images;
	}

	public void setImages(String images) {
		this.images = images;
	}

	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	public Integer getPeopleNumber() {
		return peopleNumber;
	}

	public void setPeopleNumber(Integer peopleNumber) {
		this.peopleNumber = peopleNumber;
	}

	public Double getLowConsume() {
		return lowConsume;
	}

	public void setLowConsume(Double lowConsume) {
		this.lowConsume = lowConsume;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}
 
	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public String getExplanation() {
		return explanation;
	}

	public void setExplanation(String explanation) {
		this.explanation = explanation;
	}

	public String getLinkman() {
		return linkman;
	}

	public void setLinkman(String linkman) {
		this.linkman = linkman;
	}
}
